package com.lanhao.aot.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

import org.apache.commons.io.IOUtils;

import cn.hutool.core.io.IoUtil;

public class SocketUtil {
	public static void main(String[] args) {
		String localAddress = "0.0.0.0";
		try (Socket socket = new Socket(InetAddress.getByName("192.168.2.23"), 8005,
				InetAddress.getByName(localAddress), 0)) {
			OutputStream outputStream = socket.getOutputStream();
			InputStream inputStream = socket.getInputStream();
			int i = 3;
			String cmd = "CA20" + String.format("%02d", i) + "18020100AC";
//			String cmd = "CA20" + String.format("%02d", i) + "450101AC";
			byte[] b = cmd.getBytes();
			outputStream.write(b);
			outputStream.flush();

			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
			int num = 0;
			char ch;
			char[] cbuf = new char[3];
			while ((num = reader.read(cbuf)) != -1) {
//				ch = (char) num;
//				if(ch == 'C') {
//					System.out.println("");
//				}
				if (cbuf[0] == 'C' && cbuf[1] == 'A') {
					System.out.println(">>");
				}
				
				System.out.print(cbuf);
				if (cbuf[0] == 'A' && cbuf[1] == 'C') {
					System.out.println("<<");
				}
				
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
